package gui.edit.components;

import gui.edit.enums.E_EditParamsSliders;
import gui.edit.listeners.ListenerActorsListSelection;
import gui.edit.utils.Actor;
import gui.edit.utils.ActorsMap;
import gui.edit.utils.ActorsPair;

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import tuio.components.CheckBoxTouch;
import tuio.components.SliderTouch;

import controls.DataControl;

/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class ActorsPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7281075794844058504L;

	private JList actorsJList;

	private ActorsMap actorsMap;
	
	private JPanel isolationPanel, relationsPanel;
	
	private JScrollPane relationsScrollPane;

	private JPanel relationListPanel;
	
	private DataControl dataControl;
	
	private SliderTouch visibilitySlider;
	

	public ActorsPanel(DataControl dataControl){
		this.dataControl=dataControl;
		this.actorsMap = new ActorsMap(dataControl);
		visibilitySlider = new SliderTouch();
		dataControl.getParamsControl().getVisibilityControl().setComponent(visibilitySlider);
		for(int i=0;i<dataControl.getPossibleActors().size();i++){
			actorsMap.put(dataControl.getPossibleActors().get(i),new Actor(dataControl.getPossibleActors().get(i),this));
		}
		
		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		c.gridx=0;
		c.gridy=0;
		c.fill=GridBagConstraints.BOTH;
		
		add(new ParamsSliders_StandardLabelTablePanel(visibilitySlider, dataControl.getParamsControl().getVisibilityControl(), E_EditParamsSliders.VISIBILITY));
		
		c.weightx=0;
		c.gridx+=1;
		add(buildActorsListPanel(),c);
		
		c.gridx=c.gridx+1;
		c.weightx=0.3;
		add(buildIsolationPanel(),c);
		
		c.gridx=c.gridx+1;
		c.weightx=1;
		add(buildRelationsPanel(),c);
		

	}

	public ActorsPanel() {
		super();
		// TODO Auto-generated constructor stub
	}

	private JPanel buildActorsListPanel(){
		JPanel actorsListPanel = new JPanel(new GridBagLayout());
		
		actorsListPanel.setBorder(BorderFactory.createTitledBorder("Actors"));
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.VERTICAL;

		Set<String>actorNames = actorsMap.keySet();
		actorsJList = new JList(actorNames.toArray());
		
		Font actorsFont = new Font("Serif", Font.BOLD, 12);
		actorsJList.setFont(actorsFont);
		actorsJList.addListSelectionListener(new ListenerActorsListSelection(this));
		actorsJList.setSelectionModel(new DefaultListSelectionModelKeepSelected());
		actorsJList.setVisibleRowCount(10);

		JScrollPane actorsScrollPane = new JScrollPane(actorsJList);
		actorsScrollPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		actorsListPanel.add(actorsScrollPane,c);

		return actorsListPanel;
	}
	
	private JPanel buildIsolationPanel(){
		isolationPanel = new JPanel(new GridBagLayout());
		isolationPanel.setBorder(BorderFactory.createTitledBorder("Isolation"));
		
		return isolationPanel;
	}
	
	public void updateIsolationPanel(){
		GridBagConstraints c = new GridBagConstraints();
		isolationPanel.removeAll();
		c.gridx=0;
		c.gridy=0;
		c.anchor=GridBagConstraints.ABOVE_BASELINE_LEADING;
		ArrayList<CheckBoxTouch>listBox = actorsMap.getSelectedActorsCheckBoxes();
		for(int i=0;i<listBox.size();i++){
			c.gridx=0;
			c.gridy=i;
			isolationPanel.add(listBox.get(i),c);
		}
		isolationPanel.revalidate();
	}
	
	private JPanel buildRelationsPanel(){
		relationsPanel = new JPanel(new BorderLayout());
		relationsPanel.setBorder(BorderFactory.createTitledBorder("Relations"));
		
		JLabel labelRelations = new JLabel("Dominance      Affinity      Dominance", JLabel.CENTER);
		labelRelations.setFont(new Font("SansSerif",Font.ITALIC,10));
		relationsPanel.add(labelRelations,BorderLayout.NORTH);
		
		relationListPanel = new JPanel();
		relationListPanel.setLayout(new GridBagLayout());
		
		relationsScrollPane = new JScrollPane(relationListPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		relationsPanel.add(relationsScrollPane, BorderLayout.CENTER);

		return relationsPanel;
	}
	
	public void updateRelationsPanel(){
		relationListPanel.removeAll();
		GridBagConstraints c = new GridBagConstraints();
		
		ArrayList<ActorsPair> actorPairs = actorsMap.getActorRelations();
		c.gridx=0;
		if(!actorPairs.isEmpty()){
			for(int i=0;i<actorPairs.size();i++){
				c.gridy=i;
				relationListPanel.add(new TwoActorsRelationPanel(actorPairs.get(i).getActor1(), actorPairs.get(i).getActor2()),c);
			}
		}
		relationListPanel.revalidate();
		relationsPanel.revalidate();
	}

	public void setActorsMap(ActorsMap actorsMap) {
		this.actorsMap = actorsMap;
	}

	public ActorsMap getActorsMap() {
		return actorsMap;
	}

	public void setDataControl(DataControl dataControl) {
		this.dataControl = dataControl;
	}

	public DataControl getDataControl() {
		return dataControl;
	}
}
